<template>
  <div>
    <div>
      <Row class="header" type="flex" justify="start">
        <Col span="24">
          <Icon class="icon" type="ios-podium-outline"/>
          <span class="title">年龄统计柱形图</span>
        </Col>
      </Row>
      <br>
      <div id="ageSumlineEchart" :style="{ width: width + 'px', height: height + 'px'}"></div>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
export default {
  name: 'AgeSumlineEchart',
  data () {
    return {
      chartData: []
    }
  },
  props: {
    width: {
      type: Number,
      default: () => {
        return 500
      }
    },
    height: {
      type: Number,
      default: () => {
        return 300
      }
    }
  },
  mounted () {
  },
  methods: {
    chart (data) {
      this.chartData = data
      let option = {
        title: {
          text: ''
        },
        textStyle: {
          color: '#bababa'
        },
        legend: {},
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(50,50,50,0.6)',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'line' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        dataset: {
          dimensions: ['age', 'sum'],
          source: []
        },
        yAxis: {
          name: '单位：人',
          type: 'value',
          axisLine: {
            lineStyle: {
              color: '#bababa' // 轴及其文字颜色
            }
          }
        },
        xAxis: {
          type: 'category',
          axisLabel: {
            interval: 0,
            rotate: 40
          },
          axisLine: {
            lineStyle: {
              color: '#bababa' // 轴及其文字颜色
            }
          }
        },
        grid: {
          y: 50,
          y2: 100
        },
        series: [
          {
            name: '社工人数',
            type: 'line',
            label: {
              normal: {
                show: true,
                position: 'top',
                color: ['#06aaf5']
              }
            },
            itemStyle: {
            // 通常情况下：
              normal: {
                color: ['#06aaf5']
              },
              // 鼠标悬停时：
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
      this.chartData.forEach(item => {
        // area为空的是之前导入的数据（暂不显示）
        if (item.age) {
          option.dataset.source.push(item)
        }
      })
      let myChart = echarts.init(document.getElementById('ageSumlineEchart'))
      myChart.setOption(option)
      // console.log(option)
    }
  }
}
</script>

<style scoped>
  .header {
    height: 30px;
  }

  .icon {
    font-size: 28px;
    font-weight: 600;
    margin-right: 5px;
  }

  .title {
    font-size: 16px;
    font-weight: 600;
    line-height: 30px;
  }
</style>
